#ifndef ELSWITCH_H
#define ELSWITCH_H

#include "elcomponent.h"
#include <QVariant>

class QPropertyAnimation;
// BUG: fix circle's position when change the text on left(inactive state) side
class ElSwitch : public ElComponent
{
    Q_OBJECT
public:
    // StateData indicates background color, text, text color and data respectively.
    using StateData = std::tuple<QColor, QString, QColor, QVariant>;

    enum State { Inactive, Active };

    ElSwitch(QWidget *parent = nullptr);

    void setWidth(int w);

    void setText(State state, const QString &text);
    QString text(State state) const;

    inline void setCurrentText(const QString &text) { setText(_state, text); }
    inline QString currentText() const { return text(_state); }

    void setForeground(State state, const QColor &fgColor);
    QColor foreground(State state) const;

    void setBackground(State state, const QColor &bgColor);
    QColor background(State state) const;

    void setData(State state, const QVariant &data);
    QVariant data(State state) const;

    inline void setCurrentData(const QVariant &text) { setData(_state, text); }
    inline QVariant currentData() const { return data(_state); }

    inline bool isActive() const { return _state == Active; }
    State state() const;
    void setState(State state);

public slots:
    void toggle();

signals:
    void toggled(bool isActive);
    void stateChanged(State state);

protected:
    bool event(QEvent *e) override;
    void paintEvent(QPaintEvent *e) override;
    void mouseReleaseEvent(QMouseEvent *e) override;
    void initPalette() override;

    class ElSwitchCircle : public ElComponent
    {
        friend class ElSwitch;

    public:
        explicit ElSwitchCircle(ElSwitch *parent);

        void slide();

        static const qreal Radius;

    protected:
        void paintEvent(QPaintEvent *e) override;
        void initPalette() override;

    private:
        QPropertyAnimation *_anim = nullptr;
    } *switchCircle = nullptr;

private:
    Q_PROPERTY(State state READ state WRITE setState NOTIFY stateChanged FINAL)
    Q_PROPERTY(QString currentText READ currentText WRITE setCurrentText)
    Q_PROPERTY(QVariant currentData READ currentData WRITE setCurrentData)

    static const int Init_Width;
    static const int Init_Height;

    StateData _inactive_state;
    StateData _active_state;
    State _state = Inactive;

    ///
    /// \brief _disabled_inactive_colors
    /// extra palette for inactive state since ElPalette is not enough to hold all disabled colors
    /// _palette is used to store active state(Background and Foreground)
    /// and _disabled_inactive_colors is to store inactive state([0]Background and [1]Foreground)
    ///
    QColor _disabled_inactive_colors[2];

    // used by switchCircle
    QRectF drawing_rect;

    QColor getDisabledColor(const QColor &color);
};

#endif // ELSWITCH_H
